home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / gfx / x11 / oldXlib.lha / oldXlib / src / lib / oldX / XMakeAssoc.c < prev    next >
C/C++ Source or Header  |  1996-08-31  |  4KB  |  114 lines

  1. /* $XConsortium: XMakeAssoc.c,v 10.22 94/04/17 20:11:38 rws Exp $ */
  2. /*
  3.  
  4. Copyright (c) 1985  X Consortium
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  19. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  20. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.  
  23. Except as contained in this notice, the name of the X Consortium shall not be
  24. used in advertising or otherwise to promote the sale, use or other dealings
  25. in this Software without prior written authorization from the X Consortium.
  26.  
  27. */
  28.  
  29. #include "Xlibint.h"
  30. #include "X10.h"
  31.  
  32. #ifdef AMIWIN
  33. #include <pragmas/xlib_pragmas.h>
  34. extern struct Library *XLibBase;
  35. #endif
  36.  
  37. /*
  38.  * XMakeAssoc - Insert data into an XAssocTable keyed on an XId.
  39.  * Data is inserted into the table only once.  Redundant inserts are
  40.  * meaningless (but cause no problems).  The queue in each association
  41.  * bucket is sorted (lowest XId to highest XId).
  42.  */
  43. XMakeAssoc(dpy, table, x_id, data)
  44.     register Display *dpy;
  45.     register XAssocTable *table;
  46.     register XID x_id;
  47.     register XPointer data;
  48. {
  49.     int hash;
  50.     register XAssoc *bucket;
  51.     register XAssoc *Entry;
  52.     register XAssoc *new_entry;
  53.     
  54.     /* Hash the XId to get the bucket number. */
  55.     hash = x_id & (table->size - 1);
  56.     /* Look up the bucket to get the entries in that bucket. */
  57.     bucket = &table->buckets[hash];
  58.     /* Get the first entry in the bucket. */
  59.     Entry = bucket->next;
  60.  
  61.     /* If (Entry != bucket), the bucket is empty so make */
  62.     /* the new entry the first entry in the bucket. */
  63.     /* if (Entry == bucket), the we have to search the */
  64.     /* bucket. */
  65.     if (Entry != bucket) {
  66.         /* The bucket isn't empty, begin searching. */
  67.         /* If we leave the for loop then we have either passed */
  68.         /* where the entry should be or hit the end of the bucket. */
  69.         /* In either case we should then insert the new entry */
  70.         /* before the current value of "Entry". */
  71.         for (; Entry != bucket; Entry = Entry->next) {
  72.             if (Entry->x_id == x_id) {
  73.                 /* Entry has the same XId... */
  74.                 if (Entry->display == dpy) {
  75.                     /* Entry has the same Display... */
  76.                     /* Therefore there is already an */
  77.                     /* entry with this XId and Display, */
  78.                     /* reset its data value and return. */
  79.                     Entry->data = data;
  80.                     return;
  81.                 }
  82.                 /* We found an association with the right */
  83.                 /* id but the wrong display! */
  84.                 continue;
  85.             }
  86.             /* If the current entry's XId is greater than the */
  87.             /* XId of the entry to be inserted then we have */
  88.             /* passed the location where the new XId should */
  89.             /* be inserted. */
  90.             if (Entry->x_id > x_id) break;
  91.         }
  92.         }
  93.  
  94.     /* If we are here then the new entry should be inserted just */
  95.     /* before the current value of "Entry". */
  96.     /* Create a new XAssoc and load it with new provided data. */
  97.     new_entry = (XAssoc *)Xmalloc(sizeof(XAssoc));
  98.     if (new_entry == NULL) {
  99.         /* Malloc failed! */
  100.         errno = ENOMEM;
  101.         _XIOError(dpy);
  102.     }
  103.     new_entry->display = dpy;
  104.     new_entry->x_id = x_id;
  105.     new_entry->data = data;
  106.  
  107.     /* Insert the new entry. */
  108.     new_entry->prev = Entry->prev;
  109.     new_entry->next = Entry;
  110.     Entry->prev->next = new_entry;
  111.     Entry->prev = new_entry;
  112. }
  113.  
  114.